package com.kgc.scd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.SensitiveType;
import com.kgc.scd.bean.SensitiveWord;
import com.kgc.scd.bean.SensitiveWordResult;
import com.kgc.scd.contant.CommonConstant;
import com.kgc.scd.mapper.SensitiveWordMapper;
import com.kgc.scd.service.SensitiveWordService;
import com.kgc.scd.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2022/5/17.
 * <p>
 * Author : zhukang
 * <p>
 * Description: 敏感词测试入口
 */
@Slf4j
@RestController
public class SensitiveWordController {

    @Autowired
    private SensitiveWordFilterUtil sensitiveWordFilterUtil;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Autowired(required = false)
    private SensitiveWordMapper sensitiveWordMapper;



    /**
     * @author : xzy
     * @date   : 2022/5/17
     * @param  : [java.lang.String]
     * @return : com.kgc.sbt.util.RequestResult<java.lang.String>
     * @description : 测试搜索中的敏感词
     */
    @GetMapping("/testSensitiveWord")
    @ApiOperation(value = "敏感词过滤", notes = "搜索关键字中，增加敏感词过来，限制搜索")
    public RequestResult<String> testSensitiveWord(@RequestParam String searchKey){

        // 校验搜索关键字中，是否包含敏感词，如果包含，提示错误
        if(sensitiveWordFilterUtil.isContainSensitiveWord(searchKey)){
            log.warn("------ 命中敏感词，搜索关键字：{} ------", searchKey);
            return ResultBuildUtil.fail("搜索失败，命中敏感词");
        }
        return ResultBuildUtil.success();
    }

    /**
    * @author: HarryXzy
    * @date: 2022/6/14 08:27
    * @return: com.kgc.scd.util.RequestResult<java.lang.String>
    * @description: 获取所有的敏感词，分页
    */
    @GetMapping("/allSensitiveWord")
    @ApiOperation(value = "获取所有的敏感词", notes = "获取所有的敏感词，采用分页进行展示")
    public Map<String, Object>  allSensitiveWord(@RequestParam("pageNo") int pageNo,
                                                 @RequestParam("pageSize")int pageSize){
        PageSupport<SensitiveWordResult> allSensitiveWord = sensitiveWordService.getAllSensitiveWord(pageNo, pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
        map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
        map.put("data",allSensitiveWord);
        return map;
    }

    /**
     * @author: HarryXzy
     * @date: 2022/6/14 08:27
     * @return: com.kgc.scd.util.RequestResult<java.lang.String>
     * @description: 删除敏感词
     */
    @DeleteMapping("/deleteSensitiveWord")
    @ApiOperation(value = "删除敏感词", notes = "根据敏感词，删除敏感词")
    public Map<String, Object> deleteSensitiveWord(@RequestParam("sensitiveWord")String sensitiveWord){
        HashMap<String, Object> map = new HashMap<>();
        boolean b = sensitiveWordService.deleteSensitiveWord(sensitiveWord);
        if(b){
            map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
            map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
            map.put("data","");
            return map;
        }else{
            map.put("code",503);
            map.put("msg","敏感词不存在删除失败！");
            map.put("data","");
            return map;
        }
    }

    /**
     * @author: HarryXzy
     * @date: 2022/6/14 08:27
     * @return: com.kgc.scd.util.RequestResult<java.lang.String>
     * @description: 添加敏感词
     */
    @PostMapping("/addSensitiveWord")
    @ApiOperation(value = "添加敏感词", notes = "添加敏感词，支持多个添加")
    public Map<String, Object> addSensitiveWord(@RequestParam("categoryId") String categoryId,
                                                @RequestParam("sensitiveWord") String sensitiveWord){
        HashMap<String, Object> map = new HashMap<>();
        boolean b=false;
        QueryWrapper<SensitiveWord> word = new QueryWrapper<>();
        word.eq("word",sensitiveWord);
        SensitiveWord sensitiveWord1 = sensitiveWordMapper.selectOne(word);
        if(sensitiveWord1 != null){
            map.put("code",503);
            map.put("msg","敏感词已存在，请重新插入！");
            map.put("data","敏感词已存在");
            return map;
        }
            b = sensitiveWordService.addSensitiveWord(categoryId,sensitiveWord);
        if(b){
            map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
            map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
            map.put("data",sensitiveWord);
            return map;
        }else{
            map.put("code",CommonConstant.UNIFY_RETURN_FAIL_CODE);
            map.put("msg","系统错误");
            map.put("data","");
            return map;
        }
    }

    /**
     * @author: HarryXzy
     * @date: 2022/6/14 08:27
     * @return: com.kgc.scd.util.RequestResult<java.lang.String>
     * @description: 获取敏感词种类
     */
    @PostMapping("/sensitiveWordType")
    @ApiOperation(value = "获取敏感词种类", notes = "获取敏感词种类")
    public Map<String, Object> getsensitiveWordType(){
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<SensitiveType> result= sensitiveWordService.getSensitiveWordType();
            map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
            map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
            map.put("data",result);
            return map;
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","系统错误");
            map.put("data","");
            return map;
        }


    }









}
